import { Tween } from "cc"
import { Label } from "cc"
import { _decorator, Component, Node } from "cc"
import { ClientEvent } from "../framework/ClientEvent"
const { ccclass, property } = _decorator

@ccclass("GameChatFrame")
export class GameChatFrame extends Component {

  @property(Label)
  labText:Label = null!

  private curIndex:number = 0

  // private arrText1:string[] = [
  //   "Monsters","are","mutating!","Protect","our","fortress!"
  // ]

  private text1:string = "Monsters are mutating!  Protect our fortress!"

  protected start(): void {
    this.playGameText1()
  }

  public playGameText1(){

    this.curIndex = 0
    // this.updateText1()
    this.labText.string = ""

    this.node.alpha = 0
    new Tween(this.node).to(0.25,{alpha:1}).start()
    this.schedule(this.updateText1,0.02)
  }


  private updateText1(){
    let curText:string = ""
    for(let i:number = 0;i < this.text1.length && i <= this.curIndex;i++){
      curText += this.text1[i]

    }
    this.labText.string = curText
    this.curIndex++
    if(this.curIndex >= this.text1.length){
      this.unschedule(this.updateText1)
      this.scheduleOnce(this.onDestroySelf,2)
      ClientEvent.emit("onPlayerTextComplete")

    }
  }

  onDestroySelf(){

    this.unscheduleAllCallbacks()
    this.node.destroy()
  }
}
